package com.xsq.page;

import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;

import com.jaeger.library.StatusBarUtil;
import com.xsq.R;
import com.xsq.utils.SharedPreferencesUtils;
import com.xsq.view.DelayDialog;
import com.xsq.view.LoadingDialog;

public class BaseActivity extends AppCompatActivity {

    protected SharedPreferencesUtils spUtils;
    private LoadingDialog loadingDialog;
    private DelayDialog toastDialog;
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        spUtils = new SharedPreferencesUtils(this, "DLX");
        mContext = this;
        StatusBarUtil.setTransparent(this);
    }

    protected void showToast(String message) {
        if(toastDialog == null) {
            toastDialog = new DelayDialog(this, message);
        }else {
            toastDialog.setMessage(message);
        }
        toastDialog.show();
    }

    protected void showLoading() {
        if(loadingDialog == null) {
            loadingDialog = new LoadingDialog(this, R.style.LoadingDialog);
        }
        loadingDialog.show();
    }

    protected void showLoading(String loadingText) {
        if(loadingDialog == null) {
            loadingDialog = new LoadingDialog(this, R.style.LoadingDialog);
        }
        loadingDialog.show();
        loadingDialog.setLoadingText(loadingText);
    }

    protected void hideLoading() {
        if(loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    public void hideKeyboard(IBinder token) {
        ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(token, 0);
    }

    protected boolean hasConfiged() {
        return (!TextUtils.isEmpty(getBaseUrl())) && (!TextUtils.isEmpty(getMerId())) && (!TextUtils.isEmpty(getWinId()));
    }

    protected void setConfig(String baseUrl, String merId, String winId) {
        setBaseUrl(baseUrl);
        setMerId(merId);
        setWinId(winId);
    }

    private void setBaseUrl(String baseUrl) {
        if(!TextUtils.isEmpty(baseUrl)) {
            spUtils.setObject("BASE_URL", baseUrl);
        }
    }

    protected String getBaseUrl() {
        String baseUrl = spUtils.getObject("BASE_URL", String.class);
        return baseUrl;
    }

    private void setMerId(String merId) {
        if(!TextUtils.isEmpty(merId)) {
            spUtils.setObject("MER_ID", merId);
        }
    }

    protected String getMerId() {
        String merId = spUtils.getObject("MER_ID", String.class);
        return merId;
    }

    private void setWinId(String winId) {
        if(!TextUtils.isEmpty(winId)) {
            spUtils.setObject("WIN_ID", winId);
        }
    }

    protected String getWinId() {
        String winId = spUtils.getObject("WIN_ID", String.class);
        return winId;
    }
}
